package com.myteam.mystory.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.Html;
import android.text.TextUtils;

public class Utils {

	public static boolean isNetworkAvailable(Activity activity) {
		ConnectivityManager connectivity = (ConnectivityManager) activity
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			return false;
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * Format string to plain string from escape html string
	 * 
	 * @param s
	 * @return plain string
	 */
	public static String foramtString(String s) {
		return TextUtils.isEmpty(s) ? s : Html.fromHtml(s).toString();
	}

	public static String removeHTML(String s) {
		// Remove HTML tag from java String
		String noHTMLString = s.replaceAll("\\<.*?\\>", "");
//		// Remove Carriage return from java String
		noHTMLString = noHTMLString.replaceAll("<br/>", "\n ");
		noHTMLString = noHTMLString.replaceAll("\n\n\n", "\n ");
		noHTMLString = noHTMLString.replaceAll("\n\n\n", "\n\n ");
		noHTMLString = noHTMLString.replaceAll("&nbsp;", " ");
		noHTMLString = noHTMLString.replaceAll("&quot;", "");
		noHTMLString = noHTMLString.replaceAll("&amp;", " ");
		noHTMLString = noHTMLString.replaceAll("&#039;", " ");
		noHTMLString = noHTMLString.replaceAll("  ", "");
		return noHTMLString;
	}
	public static String getTextFromFile(String file_path) {
		File file = new File(file_path);

		// Read text from file
		StringBuilder text = new StringBuilder();

		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;

			while ((line = br.readLine()) != null) {
				text.append(line);
				text.append('\n');
			}
		} catch (IOException e) {
			// You'll need to add proper error handling here
			e.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return text.toString();
	}
}